<!--vue模板-->
<template>
  <div>
    <!--搜索-->
    <div class="toolbar">
      <el-input
        placeholder="请输入部门名称"
        style="width: 200px; margin-right: 10px"
        v-model="deptname"
      ></el-input>
      <el-button type="primary" @click="init">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <!--按钮区-->
    <div class="toolbar">
      <el-button type="primary" @click="save(0, '顶级部门')">新增</el-button>
    </div>
    <!--表格数据-->
    <el-table
      :data="tableData"
      style="width: 100%"
      stripe
      border
      row-key="id"
      lazy
      :load="load"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        label="序号"
        type="index"
        width="80"
        header-align="center"
        align="center"
      />
      <el-table-column
        prop="deptCode"
        label="部门编码"
        width="180"
        header-align="center"
      />
      <el-table-column prop="name" label="部门名称" header-align="center" />
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        header-align="center"
      />
      <el-table-column
        prop="status"
        label="状态"
        width="100"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="chg(row.id, row.status)"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        header-align="center"
        align="center"
        width="180"
      >
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Plus"
            circle
            title="新增"
            @click="save(row.id, row.name)"
          />
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="编辑"
            @click="save(row.parentId, row.parentName, row.id, row.name)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            title="删除"
            @click="del(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!--新增-->
    <SaveOrUpdate
      ref="saveDept"
      :visible="isShow"
      @close="isShow = false"
      @refresh="init()"
    />
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref, onBeforeMount } from "vue";
import { Delete, Edit, Plus } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { queryDept, changeDeptStatus, deleteDept } from "@/api/dept.js";
import SaveOrUpdate from "./SaveOrUpdate.vue";

//引用保存组件
const saveDept = ref(null);
//表格数据
const tableData = ref([]);
//是否显示新增用户
const isShow = ref(false);
const deptname = ref("");

function reset() {
  deptname.value = "";
  init();
}

//新增部
function save(pid, pname, id, name) {
  //显示组件
  isShow.value = true;
  //调用组件内的初始方法
  saveDept.value.init(pid, pname, id, name);
}

//方法
async function init() {
  let params = {
    parentId: 0,
    name: deptname.value
  };
  let res = await queryDept(params);
  if (res.code == 200) {
    tableData.value = res.data;
  }
}

//加载子节点函数
async function load(row, treeNode, resolve) {
  let params = {
    parentId: row.id
  };
  let res = await queryDept(params);
  if (res.code == 200) {
    resolve(res.data);
  }
}

//删除
function del(id) {
  ElMessageBox.confirm("你确定要删除吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      //确认删除
      todel(id);
    })
    .catch(() => {});
}

async function todel(id) {
  let res = await deleteDept(id);
  if (res.code == 200) {
    ElMessage({
      message: "删除成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

async function chg(id, status) {
  console.log(id, "    ", status);
  let res = await changeDeptStatus(id, status);
  if (res.code == 200) {
    ElMessage({
      message: "修改成功",
      type: "success"
    });
    init(); //重新查询数据
  }
}

//生命周期钩子
onBeforeMount(() => {
  init();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.toolbar {
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  padding-left: 20px;
}
</style>
